from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
import datetime
from app01 import models


def index001(request):
    models.UserInfo.objects.create(name="张三", pwd="123")
    return HttpResponse("ok")

def index002(request):
    obj = models.UserInfo.objects.create(name="李四", pwd="123")
    print(obj)
    print(obj.pk)
    print(obj.id)
    print(obj.name)

    return HttpResponse("ok")

def index003(request):
    """向数据库中新增一条数据"""
    obj = models.UserInfo(name="王老五", pwd="123")
    obj.save()

    return HttpResponse("ok")

def index004(request):
    """向数据库中新增一条数据"""
    obj = models.UserInfo(name="肖老六", pwd="123")
    obj.pwd = "123456"
    obj.save()

    return HttpResponse("ok")

# Create your views here.
def auth(request):
    return HttpResponse("auth")

def login(request):
    return HttpResponse("login")

def user(request):
    return HttpResponse("user")

class Person(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age

# Create your views here.
def index(request):
    """
    1.打开 app01/test.html 文件，读取内容，返回给浏览器
    2.渲染完成，得到一个渲染完成的文本
    3.将文本返回给浏览器
    """

    context = {
        'name': 'django',
        'n2': 'zhangyz',
        'n3': [11, 22, 33, 44],
        'n4': {
            "name": "张阿狗",
            "age": 29,
        },
        'n5': Person(name='张阿狗', age=29),
        'n8': "zhangsan",
        'n9': datetime.datetime.now(),
        'n10': datetime.datetime.now().strftime("%Y-%m-%d"),
        'n11': [
            {"id": 1, "name": "zhangsan01", "age": 20},
            {"id": 2, "name": "zhangsan02", "age": 21},
            {"id": 3, "name": "zhangsan03", "age": 22},
            {"id": 4, "name": "zhangsan04", "age": 23},
        ]
    }

    return render(request, 'app01/index.html', context)
